#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include "rngs.h"

#define NOISY_TEST 1

void checkAdventurer_card() {
    printf ("RANDOM TESTING adventurer.\n");
    // cards 0 to 17 are in play
    int k[10] = {adventurer, council_room, feast, gardens, mine,
	    remodel, smithy, village, baron, great_hall};
    struct gameState post;
    int numTests = 100;
    int testCount = 0;
    int size;
    int i;
    int discardOrDeck = 0;
    int initCurrency;
    int finCurrency;

    srand(time(NULL));
    while (testCount < numTests) {
        initCurrency = 0;
        finCurrency = 0;
        initializeGame(2, k, 1, &post);
        for (i = 0; i < post.handCount[0]; i++) {
            if (post.hand[0][i] == 4) {
                initCurrency++;
            }
        }
        size = rand() % 50 + 5;
        // throw cards in discard pile or deck
        discardOrDeck = rand() % 2;
        if (discardOrDeck) {
            post.discardCount[0] = size;
            // set current deck to all curses
            for (i = 0; i < post.deckCount[0]; i++) {
                post.deck[0][i] = 0;
            }
            for (i = 0; i < (size-2); i++) {
                post.discard[0][i] = rand() % 17;
            }
            // ensure atleast two currency exist
            post.discard[0][(size-1)] = 4;
            post.discard[0][(size-2)] = 4;
        } else {
            post.deckCount[0] = size;
            for (i = 0; i < (size-2); i++) {
                post.deck[0][i] = rand() % 17;
            }
            post.deck[0][(size-1)] = 4;
            post.deck[0][(size-2)] = 4;
        }
        adventurer_card(0, &post, 0);
        printf("Expected handCount: 6, Actual: %d\n", post.handCount[0]);
        assert(post.handCount[0] == 6);
        for (i = 0; i < post.handCount[0]; i++) {
            if ((post.hand[0][i] > 3) & (post.hand[0][i] < 7)) {
                finCurrency++;
            }
        }
        printf("Initial currency in hand: %d, after adventurer: %d\n",
        initCurrency, finCurrency);
        assert(finCurrency > initCurrency);
        testCount++;
    }
}


int main () {
  checkAdventurer_card();
  printf ("ALL TESTS OK\n");
  return 0;
}
